package com.danceforyou.user.controller;

import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.common.vo.SqlOrderEnum;
import com.danceforyou.user.model.RoleDO;
import com.danceforyou.user.model.UserDO;
import com.danceforyou.user.service.MenuService;
import com.danceforyou.user.service.RoleMenuService;
import com.danceforyou.user.service.RoleService;
import com.danceforyou.user.vo.MenuVO;
import com.danceforyou.user.vo.RoleVO;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 类名称：RoleController
 * 创建人：danceforyou
 * 创建时间：2020年04月16日
 * 版本号：1.0v
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleMenuService roleMenuService;
	@Autowired(required = false)
	private HttpSession session;

    /**
	 * 新增记录
	 * @param role 记录信息
     * @return boolean
     */
	@RequestMapping("/saveInfo")
	public boolean saveInfo(RoleDO role) {
        roleService.saveInfo(role);
        return true;
	}

    /**
	 * 修改记录
	 * @param role 记录信息
     * @return boolean
     */
	@RequestMapping("/updateInfo")
	public boolean updateInfo(RoleDO role) {
        roleService.updateInfo(role);
        return true;
	}

    /**
	 * 删除记录
	 * @param ids 记录ID
     * @return int
     */
	@RequestMapping("/deleteInfoByIds")
	public int deleteInfoByIds(String ids) {
        return roleService.deleteInfoByIds(ids);
	}

    /**
	 * 根据ID获取信息
	 * @param id 记录ID
	 * @return RoleDO
     */
	@RequestMapping("/getInfoById")
	public RoleDO getInfoById(Long id) {
        RoleDO role = roleService.getById(id);
		AssertUtil.notNull(role, "信息不存在");
		return role;
	}

    /**
     * 获取岗位列表
     * @return List<RoleVO>
     */
    @RequestMapping("/getRoleList")
	public List<RoleVO> getRoleList() {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        List<RoleVO> result = new ArrayList<>();
        long[] departmentArr = new long[]{Constants.ROLE_GLB_ID, Constants.ROLE_MJB_ID, Constants.ROLE_XSB_ID, Constants.ROLE_CWB_ID};
        for (long department : departmentArr) {
            List<RoleDO> roleList = roleService.listByProperty("pId", department, "id", SqlOrderEnum.ASC);
            for (RoleDO role : roleList) {
                RoleVO roleVO = new RoleVO(role);
                roleVO.setDepartment(department);
                result.add(roleVO);
                //获取下级岗位
                List<RoleDO> subRoleList = roleService.listByProperty("pId", role.getId(), "id", SqlOrderEnum.ASC);
                for (RoleDO subRole : subRoleList) {
                    roleVO = new RoleVO(subRole);
                    roleVO.setDepartment(department);
                    result.add(roleVO);
                }
            }
        }
        return result;
    }

    /**
     * 获取父级岗位列表
     * @return List<RoleVO>
     */
    @RequestMapping("/getParentRoleList")
    public List<RoleVO> getParentRoleList() {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        //获取默认的部门
        List<RoleDO> roleList = roleService.listByProperty("pId", 0);
        List<RoleVO> result = new ArrayList<>();
        for (RoleDO role : roleList) {
            RoleVO roleVO = new RoleVO(role);
            //获取该部门一级岗位
            List<RoleDO> subRoleList = roleService.listByProperty("pId", role.getId());
            List<RoleVO> subRoleVOList = new ArrayList<>();
            for (RoleDO subRole : subRoleList) {
                subRoleVOList.add(new RoleVO(subRole));
            }
            roleVO.setChildren(subRoleVOList);
            result.add(roleVO);
        }
        return result;
    }

    /**
     * 获取菜单列表（岗位对应菜单使用）
     * @param roleId 岗位ID
     * @return List<MenuDO>
     */
    @RequestMapping("getMenuListByRoleId")
    public List<MenuVO> getMenuListByRoleId(Long roleId) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        return menuService.getMenuListByRoleId(roleId);
    }

    /**
     * 保存岗位对菜单
     * @param roleId 岗位ID
     * @param menuIds 菜单IDS
     * @return boolean
     */
    @RequestMapping("saveRoleMenu")
    public boolean saveRoleMenu(Long roleId, String menuIds) {
        roleMenuService.saveRoleMenu(roleId, menuIds);
        return true;
    }
}
